﻿using UnityEngine;
using System.Collections;
using System;

namespace Inlycat
{
    /// <summary>
    /// 窗口 
    /// </summary>

    public class PopWin : MonoBehaviour
    {
        /// <summary>
        /// 窗口名
        /// </summary>
        public PopWinName WinName;

        /// <summary>
        /// 窗口打开效果
        /// </summary>
        public OpenEffect OpenEffect = OpenEffect.None;

        /// <summary>
        /// 窗口关闭效果
        /// </summary>
        public CloseEffect CloseEffect = CloseEffect.None;

        /// <summary>
        /// 打开窗口后执行方法
        /// </summary>
        private Action _afterOpenFunc;

        /// <summary>
        /// 关闭窗口后执行方法
        /// </summary>
        private Action _afterCloseFunc;

        /// <summary>
        /// 窗口是否已经打开
        /// </summary>
        private bool _isOpened;

        /// <summary>
        /// 窗口是否已经打开
        /// </summary>
        public bool IsOpened
        {
            get { return _isOpened; }
        }

        /// <summary>
        /// 打开窗口
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="checkPreOpen">是否检查预加载</param>
        public void Open(object data = null, bool checkPreOpen = true)
        {
            if (IsOpened)
            {
                return;
            }

            if (checkPreOpen && !PreOpen())
            {
                return;
            }

            _isOpened = true;

            Action afterOpen = () =>
            {
                if (_afterOpenFunc != null)
                {
                    _afterCloseFunc();
                }
                _afterCloseFunc = null;
            };

            switch (OpenEffect)
            {
                case OpenEffect.None:
                    gameObject.SetActive(true);
                    OnOpen(data);
                    afterOpen();
                    break;
            }
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        public void Close()
        {
            if (!IsOpened)
            {
                return;
            }

            PreClose();

            _isOpened = false;

            Action afterClose = () =>
            {
                if (_afterCloseFunc != null)
                {
                    _afterCloseFunc();
                }
                _afterCloseFunc = null;
            };

            switch (CloseEffect)
            {
                case CloseEffect.None:
                    gameObject.SetActive(false);
                    OnClose();
                    afterClose();
                    break;
            }
        }

        /// <summary>
        /// 预加载窗口
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>是否打开窗口</returns>
        public virtual bool PreOpen(object data = null)
        {
            return OpenCondition(data);
        }

        /// <summary>
        /// 打开窗口后方法
        /// </summary>
        /// <param name="data">数据</param>
        public virtual void OnOpen(object data = null)
        {
        }

        /// <summary>
        /// 预关闭窗口
        /// </summary>
        /// <returns>是否关闭窗口</returns>
        public virtual bool PreClose()
        {
            return true;
        }

        /// <summary>
        /// 关闭窗口后方法
        /// </summary>
        public virtual void OnClose()
        {
        }

        /// <summary>
        /// 设置打开窗口后执行方法
        /// </summary>
        /// <param name="action">执行方法</param>
        public void SetAfterOpen(Action action)
        {
            _afterOpenFunc = action;
        }

        /// <summary>
        /// 设置关闭窗口后执行方法
        /// </summary>
        /// <param name="action">执行方法</param>
        public void SetAfterClose(Action action)
        {
            _afterCloseFunc = action;
        }

        /// <summary>
        /// 窗口打开条件
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>是否可以打开</returns>
        protected virtual bool OpenCondition(object data = null)
        {
            return true;
        }
    }

    /// <summary>
    /// 窗口名
    /// </summary>
    public enum PopWinName
    {
    }

    /// <summary>
    /// 打开效果
    /// </summary>
    public enum OpenEffect
    {
        None
    }

    /// <summary>
    /// 关闭效果
    /// </summary>
    public enum CloseEffect
    {
        None
    }
}